package qdu.edu.com.fushanf4.service.count.usertype.search2characters.splicingtwocharacters;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.ArrayList;

public class SplicingTwoCharactersMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    private final Text keyOut = new Text();
    private final ArrayList<String> arrayList = new ArrayList<>();


    @Override
    protected void map(LongWritable key, Text value, Context context) {
        String line = value.toString();
        String[] split = line.split("\t");
        arrayList.add(split[0]);
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        // 将两个汉字拼接为一个词
        int index = 0;
        for (int i=0; i<arrayList.size(); i++) {
            if (arrayList.get(index).equals("")) {  // 出现过内容为空的字符串，导致与我们预期的文件输出不符，所以此处添加该语句
                arrayList.remove(index);
                break;
            }
        }
        while (index < arrayList.size()) {
            for (String behind : arrayList) {
                keyOut.set(arrayList.get(index) + behind);
                context.write(keyOut, NullWritable.get());
            }
            index++;
        }
    }
}
